<template>
    <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="title" :width="width" :visible="visible" destroyOnClose>
    <CaseAuditForm ref="registerForm" @ok="submitCallback" :formDisabled="disableSubmit" :formBpm="false"></CaseAuditForm>
    <template #footer>
      <a-button type="primary" @click="handlePass"> 合格</a-button>
      <a-button type="primary" danger @click="handExecuteFail"> 不合格</a-button>
      <a-button @click="handleCancel"> 取消</a-button>
    </template>
  </BasicDrawer>
  <CommitDefectModal ref="registerModal" @success="handleSuccess"></CommitDefectModal>
</template>

<script lang="ts" setup>
import {computed, defineComponent, defineExpose, nextTick, ref, unref, reactive} from 'vue';
import {BasicDrawer, useDrawerInner} from '/@/components/Drawer';
import CaseAuditForm from './CaseAuditForm.vue'
import CommitDefect from './CommitDefect.vue'
import CommitDefectModal from "@/views/tm/defectManage/components/CommitDefectModal.vue";

const emit = defineEmits(['register', 'success']);
const title = ref<string>('');
const width = ref<number>(1200);
const visible = ref<boolean>(false);
const disableSubmit = ref<boolean>(false);
const registerForm = ref();
const registerModal = ref();
const isUpdate = ref(true);
const formData = ref({
    projectId: '',   
    batchId: '',  
    testStageId: '', 
    systemId: '',   
    systemModuleId: '',   
    functionName: '',   
    defectTitle: '',   
    description: '',
    defectImg: '' 
  });

const [registerDrawer,{ closeDrawer }] = useDrawerInner((data) => {
  // 方式1
  isUpdate.value = !!data?.isUpdate;
  disableSubmit.value = data.disableSubmit;
  formData.value.projectId = data.record.projectId;
  formData.value.systemId = data.record.systemId;
  formData.value.testStageId = data.record.testStageId;
  formData.value.batchId = data.record.batchId;
  console.log('useDrawerInner:'+JSON.stringify(formData.value))
  if (unref(isUpdate)) {
    edit(data.record);
  } else{
    add();
  }
});
/**
 * 新增
 */
function add() {
  title.value = '新增';
  visible.value = true;
  nextTick(() => {
    registerForm.value.add();
  });
}

/**
 * 编辑
 * @param record
 */
function edit(record) {
  title.value = disableSubmit.value ? '详情' : '用例审计';
  visible.value = true;
  nextTick(() => {
    registerForm.value.edit(record);
  });
}

/**
 * 通过
 */
function handlePass() {
  title.value = '测试执行';
  visible.value = true;
  nextTick(() => {
     registerForm.value.handleExecuteStatus("通过");
     registerForm.value.submitForm();
  });

}

function handleSuccess() {
  title.value = '测试执行';
  visible.value = true;
  nextTick(() => {
     registerForm.value.handleExecuteStatus("不通过");
     registerForm.value.submitForm();
  });
 
}
/**
 * 不通过
 */
function handExecuteFail() {
  console.log('handExecuteFail不通过:' + JSON.stringify(formData.value));
  nextTick(() => {
    //提交缺陷
    formData.value.systemModuleId = registerForm.value.formData.systemModuleId,   
    formData.value.functionName = registerForm.value.formData.functionName  
    formData.value.defectTitle = registerForm.value.formData.actualResult
    formData.value.description = registerForm.value.formData.description
    formData.value.defectImg = registerForm.value.formData.executeRecordPic
    registerModal.value.disableSubmit = false;
    registerModal.value
    registerModal.value.addDefect(formData.value);
  });
  
}
/**
 * form保存回调事件
 */
function submitCallback() {
  handleCancel();
  emit('success');
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  visible.value = false;
}

defineExpose({
  add,
  edit,
  disableSubmit,
});
</script>

<style>
/**隐藏样式-modal确定按钮 */
.jee-hidden {
  display: none !important;
}
</style>
